package me.zhengjie.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TreeDataUtil {

    public static List<Map<String,Object>> getChildren(Map<String,Object> root, List<Map<String,Object>> all){
//        System.out.println("==root=="+root.get("id"));
//        System.out.println("==all=="+all);

//        List<Map<String,Object>> listresult = all.stream().filter(m -> Objects.equals(m.get("parentId"),root.get("id"))).collect(Collectors.toList());
//        System.out.println("=====listresult======"+listresult);
        List<Map<String,Object>> list1 = all.stream().filter(m -> Objects.equals(m.get("parentId"),root.get("id"))).peek(
                (m) -> m.put("childList",(getChildren(m,all)))
        ).collect(Collectors.toList());
//        System.out.println("==list1=="+list1);
        return list1;
    }

    public static Map<String, Object> convertObjectToMap(Object obj) {

        Map<String, Object> map = new HashMap<>();
        Class<?> clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object value = field.get(obj);
                map.put(field.getName(), value);
            } catch (IllegalAccessException e) {
//                log.error("exception message",e);
                System.out.println(e);
            }
        }
        return map;
    }

    public static List<Map<String,Object>> getResultList(List<Map<String,Object>> all){
        for (Map<String, Object> map : all) {
            // 在这里处理map数据
            System.out.println("==map=="+map);
            // 调用自身进行递归操作
            if (map.containsKey("childList")) {
                List<Map<String, Object>> currentList = JSON.parseObject(JSON.toJSONString(map.get("childList")), new TypeReference<List<Map<String, Object>> >() {
                });
                for (Map<String, Object> map2 : currentList){
                    map2.put("childList","");
                }
                map.put("childList",currentList);
            }
        }

        return all;
    }

    public static List<Map<String,Object>> getTreeList(List<Map<String,Object>> all){
        int indexToRemove = 0;
        for (Map<String, Object> map : all) {
            // 在这里处理map数据

            // 调用自身进行递归操作
            if (map.containsKey("childList")) {
                List<Map<String, Object>> currentList = JSON.parseObject(JSON.toJSONString(map.get("childList")), new TypeReference<List<Map<String, Object>> >() {
                });
                System.out.print("===2===="+map );
                for (Map<String, Object> map2 : currentList){

                    List<Map<String, Object>> resultList = JSON.parseObject(JSON.toJSONString(map2.get("childList")), new TypeReference<List<Map<String, Object>>>() {
                    });

                    if(resultList.isEmpty()){
                        map.remove("childList");
                    }
                    map2.put("childList",resultList);

                }
                map.put("childList",currentList);
            }
            if(!map.containsKey("childList")){
                map.clear();
                all.remove(indexToRemove);
            }
        }
        all.removeIf(Objects::isNull);
        return all;
    }



}
